﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using TSqlServer;

/// <summary>
/// TBBSTopic 的摘要说明
/// </summary>
public class TBBSTopic : TData
{
    const string CTableName = "t_bbs_topic";
    const string CKeyName = "topic_id";

    private string FTopicID;
    private string FTopicBID;
    private string FTopicTitle;
    private string FTopicContent;
    private string FTopicAuthor;
    private DateTime FTopicTime;
    private DateTime FTopicTime2;
    private int FTopicSort;
    private int FTopicFlag;
    private int FTopicHit;
    private int FTopicForums;
    private int FTopicTop;
    private int FTopicGood;
    private int FIId;

    //分页显示使用
    public System.Data.SqlClient.SqlDataReader RResults;
    public int RowCounts;
    public int PageCounts;
    const int PageSize = 20;

    public TBBSTopic()
    {
        FTableName = CTableName;
        FKeyName = CKeyName;
    }

    public TBBSTopic(string ID)
    {
        FTableName = CTableName;
        FKeyName = CKeyName;
        FKeyValue = ID;
    }

    /// <summary>
    /// （静态）根据编号在数据库中删除记录。
    /// </summary>
    public static void DeleteByID(string ID)
    {
        TData.DeleteByID(ID, CTableName, CKeyName);
    }

    /// <summary>
    /// （静态）点击数自增。
    /// </summary>
    public static void IncHit(string ID)
    {
        string strSql = "update " + CTableName + " set topic_hit=topic_hit+1 where " + CKeyName + "='" + ID + "'";
        TSqlDB.ExecuteNonQuery(strSql);
    }

    /// <summary>
    /// （静态）回复数自计算。
    /// </summary>
    public static void MarkForumCount(string ID)
    {
        string sSql = "select count(*) from t_bbs_forum where forum_tid='" + ID + "'";
        int iCount = TSqlDB.ExecuteScalar(sSql);

        string strSql = "update " + CTableName + " set topic_forums=" + iCount.ToString() + " where " + CKeyName + "='" + ID + "'";
        TSqlDB.ExecuteNonQuery(strSql);
    }

    /// <summary>
    /// （静态）改变状态,0=正常,1=隐藏。
    /// </summary>
    public static void ChgFlag(string ID, int iFlag)
    {
        string strSql = "update " + CTableName + " set topic_flag=" + iFlag.ToString() + " where " + CKeyName + "='" + ID + "'";
        TSqlDB.ExecuteNonQuery(strSql);
    }

    /// <summary>
    /// （静态）置顶,0=正常,1=置顶。
    /// </summary>
    public static void ChgTop(string ID, int iFlag)
    {
        string strSql = "update " + CTableName + " set topic_top=" + iFlag.ToString() + " where " + CKeyName + "='" + ID + "'";
        TSqlDB.ExecuteNonQuery(strSql);
    }

    /// <summary>
    /// （静态）精华,0=正常,1=精华。
    /// </summary>
    public static void ChgGood(string ID, int iFlag)
    {
        string strSql = "update " + CTableName + " set topic_good=" + iFlag.ToString() + " where " + CKeyName + "='" + ID + "'";
        TSqlDB.ExecuteNonQuery(strSql);
    }

    /// <summary>
    /// 在数据库中检索记录（已重写）。
    /// </summary>
    public override void Select()
    {
        string strSql = GetSelectString();
        TSqlDB oDB = new TSqlDB(strSql);
        if (!oDB.EOF)
        {
            FTopicID = oDB["topic_id"].ToString();
            FTopicBID = oDB["topic_bid"].ToString();
            FTopicTitle = oDB["topic_title"].ToString();
            FTopicContent = oDB["topic_content"].ToString();
            FTopicAuthor = oDB["topic_author"].ToString();
            FTopicTime = oDB["topic_time"].ToDateTime();
            FTopicTime2 = oDB["topic_time2"].ToDateTime();
            FTopicSort = oDB["topic_sort"].ToInt();
            FTopicFlag = oDB["topic_flag"].ToInt();
            FTopicHit = oDB["topic_hit"].ToInt();
            FTopicForums = oDB["topic_forums"].ToInt();
            FTopicTop = oDB["topic_top"].ToInt();
            FTopicGood = oDB["topic_good"].ToInt();
            FIId = oDB["iid"].ToInt();
        }
        oDB.Free();
    }

    /// <summary>
    /// 在数据库中插入记录。
    /// </summary>
    public void Insert()
    {
        string ExSql = "newid(), '" + FTopicBID + "','" + FTopicTitle + "','" + FTopicContent + "','" + FTopicAuthor + "',getdate(),getdate(),0,0,0,0,0,0";
        base.Insert(ExSql);
    }

    /// <summary>
    /// 在数据库中更新记录。
    /// </summary>
    public void Update()
    {
        string ExSql = "topic_time2=getdate(), topic_title='" + FTopicTitle + "', topic_content='" + FTopicContent + "'";
        base.Update(ExSql);
    }

    //分页显示
    public void GetPage(int PageNum)
    {
        int iStartRow, iStopRow;

        iStartRow = (PageNum - 1) * PageSize + 1;
        iStopRow = PageNum * PageSize;

        TSqlDBProc oProc = new TSqlDBProc("ms_GetTopicsPage", 0);
        oProc.ms_GetTopicsPage(iStartRow, iStopRow);

        RResults = oProc.FDataReader;
    }

    //生成pagebar
    public string GenPagebar(int PageNum)
    {
        string result = "<div class=\"pagebox\">";

        CountPage();

        if (PageCounts > 1)
        {
            if (PageNum == 1)//第一页
            {
                result += "<span class=\"pagebox_num_nonce\"><a>首页</a></span>";
                for (int i = 1; i <= PageCounts; i++)
                {
                    if (i != PageNum)
                    {
                        result += "<span class=\"pagebox_num\"><a onclick=\"gettopics('" + i.ToString() + "')\">" + i.ToString() + "</a></span>";
                    }
                    else
                    {
                        result += "<span class=\"pagebox_num_nonce\"><a>" + i.ToString() + "</a></span>";
                    }
                }
                result += "<span class=\"pagebox_pre_nolink\">上一页</span>";
                result += "<span class=\"pagebox_next\"><a onclick=\"gettopics('2')\">下一页</a></span>";
                result += "<span class=\"pagebox_num\"><a onclick=\"gettopics('" + PageCounts.ToString() + "')\">尾页</a></span>";
            }
            if ((PageNum > 1) && (PageNum < PageCounts))
            {
                result += "<span class=\"pagebox_num\"><a onclick=\"gettopics('1')\">首页</a></span>";
                for (int i = 1; i <= PageCounts; i++)
                {
                    if (i != PageNum)
                    {
                        result += "<span class=\"pagebox_num\"><a onclick=\"gettopics('" + i.ToString() + "')\">" + i.ToString() + "</a></span>";
                    }
                    else
                    {
                        result += "<span class=\"pagebox_num_nonce\"><a>" + i.ToString() + "</a></span>";
                    }
                }
                result += "<span class=\"pagebox_pre\"><a onclick=\"gettopics('" + (PageNum - 1).ToString() + "')\">上一页</a></span>";
                result += "<span class=\"pagebox_next\"><a onclick=\"gettopics('" + (PageNum + 1).ToString() + "')\">下一页</a></span>";
                result += "<span class=\"pagebox_num\"><a onclick=\"gettopics('" + PageCounts.ToString() + "')\">尾页</a></span>";
            }

            if (PageNum == PageCounts)//最后一页
            {
                result += "<span class=\"pagebox_num\"><a onclick=\"gettopics('1')\">首页</a></span>";
                for (int i = 1; i <= PageCounts; i++)
                {
                    if (i != PageNum)
                    {
                        result += "<span class=\"pagebox_num\"><a onclick=\"gettopics('" + i.ToString() + "')\">" + i.ToString() + "</a></span>";
                    }
                    else
                    {
                        result += "<span class=\"pagebox_num_nonce\"><a>" + i.ToString() + "</a></span>";
                    }
                }
                result += "<span class=\"pagebox_pre\"><a onclick=\"gettopics('" + (PageCounts - 1).ToString() + "')\">上一页</a></span>";
                result += "<span class=\"pagebox_next_nolink\">下一页</span>";
                result += "<span class=\"pagebox_num_nonce\"><a>尾页</a></span>";
            }
        }
        else
        {
            result += "<span class=\"pagebox_num_nonce\"><a>首页</a></span>";
            result += "<span class=\"pagebox_num_nonce\"><a>1</a></span>";
            result += "<span class=\"pagebox_pre_nolink\">上一页</span>";
            result += "<span class=\"pagebox_next_nolink\">下一页</span>";
            result += "<span class=\"pagebox_num_nonce\"><a>尾页</a></span>";
        }

        result += "<span style=\"padding-top: 4px;\">&nbsp;(每页显示" + PageSize.ToString() + "行)</span>";
        result += "</div>";

        return result;
    }

    //计算可分页数
    private void CountPage()
    {
        string sSql = "select count(*) from t_bbs_topic where topic_flag=0";
        RowCounts = TSqlDB.ExecuteScalar(sSql);

        if (RowCounts % PageSize == 0)  //余数为零
        {
            PageCounts = RowCounts / PageSize;
        }
        else
        {
            PageCounts = (int)(RowCounts / PageSize) + 1;
        }
    }

    public string TopicBID
    {
        get
        {
            return FTopicBID;
        }
        set
        {
            FTopicBID = value;
        }
    }

    public string TopicTitle
    {
        get
        {
            return FTopicTitle;
        }
        set
        {
            FTopicTitle = value;
        }
    }

    public string TopicContent
    {
        get
        {
            return FTopicContent;
        }
        set
        {
            FTopicContent = value;
        }
    }

    public string TopicAuthor
    {
        get
        {
            return FTopicAuthor;
        }
        set
        {
            FTopicAuthor = value;
        }
    }

    public string TopicTime
    {
        get
        {
            return FTopicTime.ToString("yy-MM-dd HH:mm");
        }
    }

    public int TopicHit
    {
        get
        {
            return FTopicHit;
        }
        set
        {
            FTopicHit = value;
        }
    }

    public int IId
    {
        get
        {
            return FIId;
        }
        set
        {
            FIId = value;
        }
    }
}
